package com.industry.framework.core.generic.table;

import com.industry.framework.core.annountions.record.IdooRecordProperty;
import com.industry.framework.core.generic.record.RecordScanner;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author nbZhuozh
 * @Date 2025-04-18  09:48
 **/

public class TableConfigInfoBuilder {

    public static Map<Class<?> , TableConfigInfo> TABLE_PAGE_CONFIG_CACHE = new ConcurrentHashMap();

    public static TableConfigInfo builder(Class<?> targetType){
        if(TABLE_PAGE_CONFIG_CACHE.containsKey(targetType)){
            return TABLE_PAGE_CONFIG_CACHE.get(targetType);
        }
        if(!RecordScanner.cacheRecordMap.containsKey(targetType)){
            return new TableConfigInfo();
        }
        List<RecordScanner.RecordPropertyFields> fields = RecordScanner.cacheRecordMap.get(targetType);
        List<TableConfigInfo.TableFields> selectField = new Vector<>();
        List<TableConfigInfo.TableFields> tableField = new Vector<>();
        for (RecordScanner.RecordPropertyFields field : fields) {
            Field source = field.getTempField();
            //获取配置的注解信息
            IdooRecordProperty annotation = source.getAnnotation(IdooRecordProperty.class);
            TableConfigInfo.TableFields f = new TableConfigInfo.TableFields();
            f.setField(source.getName());
            f.setTitle(annotation.value());
            f.setFieldType(source.getType().getSimpleName());
            if(!StringUtils.isEmpty(annotation.codeKey())){
                f.setCodeKey(annotation.codeKey());
            }
            //todo 如果字段处于码表 ， 搜索框 点击时 应该展示对应所有码值 供用户搜索
            if(annotation.idooSelector()){ selectField.add(f);}
            if(annotation.idooTableList()){ tableField.add(f);}
        }
        TableConfigInfo res = new TableConfigInfo();
        res.setTableColumnsConfig(tableField);
        res.setSelectorFieldsConfig(selectField);
        TABLE_PAGE_CONFIG_CACHE.put(targetType,res);
        return res;
    }

}
